package com.example.listview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private ListView mListView;
    //商品名称与价格数据集合
    private String[] titles = {"平板电脑","智能手机","耳机","笔记本电脑","鼠标","游戏手机","M4A1","HK416","AK47"};
    private String[] prices = {"2499元","5999元","349元","4699元","119元","4299元","25000元","56000元","2000元"};

    //图片数据集合
    private int[] icons = {R.drawable.ipad,R.drawable.iphone11,R.drawable.earphones,R.drawable.magicbook,R.drawable.mouse,R.drawable.rog3,R.drawable.m4a1,R.drawable.hk416,R.drawable.ak47};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = (ListView)findViewById(R.id.lv);  //找到ListView控件
        //创建Adapter实例
        MyBaseAdapter myBaseAdapter = new MyBaseAdapter();
        mListView.setAdapter(myBaseAdapter);  //给ListView添加适配器

    }

    class MyBaseAdapter extends BaseAdapter{

        @Override
        public int getCount() {  //获取item的总数
            return titles.length;  //返回ListView中item的总数
        }

        @Override
        public Object getItem(int position) {
            return titles[position];   //获取指定位置的item对象
        }

        @Override
        public long getItemId(int position) {
            return position;     //返回item的id
        }

        //获取item的view视图
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            /*//加载list_item.xml布局文件
            View view = View.inflate(MainActivity.this,R.layout.list_item,null);
            TextView title = (TextView)view.findViewById(R.id.title);
            TextView price = (TextView)view.findViewById(R.id.price);
            ImageView iv = (ImageView)view.findViewById(R.id.iv);
            title.setText(titles[position]);  //设置标题
            price.setText(prices[position]);   //设置价格
            iv.setBackgroundResource(icons[position]);   //设置图片

            return view;*/
            //优化，解决快速滑动卡顿的问题
            ViewHolder holder = null;
            if(convertView == null){   //若空则创造ViewHolder对象
                //将list_item.xml文件找出来并转换为View对象
                convertView = View.inflate(MainActivity.this,R.layout.list_item,null);
                //找到list_item.xml中创建的TextView
                holder = new ViewHolder();
                holder.title = (TextView)convertView.findViewById(R.id.title);
                holder.price = (TextView)convertView.findViewById(R.id.price);
                holder.iv = (ImageView)convertView.findViewById(R.id.iv);
                convertView.setTag(holder);    //将holder对象添加到convertView对象中
            }else{
                //获取缓存在convertView对象中的ViewHolder对象
                holder = (ViewHolder)convertView.getTag();

            }
            holder.title.setText(titles[position]);
            holder.price.setText(prices[position]);
            holder.iv.setBackgroundResource(icons[position]);

            return convertView;
        }
    }

    //缓存item对象
    class ViewHolder{
        TextView title,price;
        ImageView iv;
    }


}